package com.shroo8.footballbloglib.dao.footballblogread;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.sql.DataSource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.SqlReturnResultSet;
import org.springframework.stereotype.Repository;

import com.shroo8.footballblog.dto.api.CommentDto;
import com.shroo8.footballblog.dto.api.UserDto;
import com.shroo8.footballbloglib.dao.constants.DataAccessConstants;
import com.shroo8.footballbloglib.dao.util.DaoUtil;

@Repository(value="getCommentsDao")
public class GetCommentsDao {
	
	@SuppressWarnings("rawtypes")
	private class OneRowMapper implements RowMapper{
	        public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
	        	CommentDto commentDto = new CommentDto();
	        	UserDto userDto = new UserDto();
	        	commentDto.setCommentId(rs.getString(DataAccessConstants.COMMENTS_COMMENT_ID));
	        	commentDto.setArticleId(rs.getString(DataAccessConstants.COMMENTS_ARTICLE_ID));
	        	userDto.setId(rs.getString(DataAccessConstants.COMMENTS_USER_ID));
	        	commentDto.setCommentText(rs.getString(DataAccessConstants.COMMENTS_COMMENT_TEXT));
	        	commentDto.setCreatedOn(rs.getTimestamp(DataAccessConstants.COMMENTS_CREATED_ON).toString());
	        	userDto.setId(rs.getString(DataAccessConstants.USERS_USER_ID));
	        	userDto.setType(rs.getString(DataAccessConstants.USERS_USER_TYPE));
	        	userDto.setExternalId(rs.getInt(DataAccessConstants.USERS_USER_EXTERNAL_ID));
	        	userDto.setScreenName(rs.getString(DataAccessConstants.USERS_USER_SCREEN_NAME));
	        	userDto.setName(rs.getString(DataAccessConstants.USERS_USER_NAME));
	        	userDto.setPicUrl(rs.getString(DataAccessConstants.USERS_USER_PIC_URL));
	        	userDto.setBigPicUrl(rs.getString(DataAccessConstants.USERS_USER_BIG_PIC_URL));
	        	userDto.setMiniPicUrl(rs.getString(DataAccessConstants.USERS_USER_MINI_PIC_URL));
	        	userDto.setCreatedOn(rs.getTimestamp(DataAccessConstants.USERS_CREATED_ON).toString());
	        	commentDto.setCommentor(userDto);
	            return commentDto;
	        }
	    }
	
	protected final String sql = DataAccessConstants.SP_GET_ARTICLE_COMMENTS;

	@Resource(name="dataSourceFootballblogRead")
	protected DataSource dataSource;

	@Resource(name="daoUtil")
	protected DaoUtil daoUtil;

    /* stored procedure parameter list
    (
    		in_article_id VARCHAR(36)
    )
	*/
	public ArrayList<CommentDto> execute(String articleId){
		
		List<SqlParameter> sqlParameters = new ArrayList<SqlParameter>();
	    Map<String, Object> inParams = new HashMap<String, Object>();

        sqlParameters.add(new SqlReturnResultSet("resultSet",
                new OneRowMapper()));
	    sqlParameters.add(new SqlParameter(DataAccessConstants.COMMENTS_ARTICLE_ID, Types.VARCHAR));
        inParams.put(DataAccessConstants.COMMENTS_ARTICLE_ID, articleId);

        Map<String, Object> out = daoUtil.spExecute(dataSource, sql,
                sqlParameters, inParams);

		@SuppressWarnings("unchecked")
		ArrayList<CommentDto> list = (ArrayList<CommentDto>) out.get("resultSet");
        return list;
	}

}
